let audioPlayer = null; // 全局音频实例（避免重复创建）

function initMusic() {
    const fileInput = document.getElementById('music-file');
    const audioElement = document.getElementById('music-player');
    const playBtn = document.getElementById('music-play');
    const pauseBtn = document.getElementById('music-pause');

    audioPlayer = audioElement;

    fileInput.onchange = e => {
        const file = e.target.files[0];
        if (!file) return;
        audioPlayer.src = URL.createObjectURL(file);
    };

    playBtn.onclick = () => audioPlayer.play().catch(err => console.error('播放失败', err));
    pauseBtn.onclick = () => audioPlayer.pause();
}

function cleanupMusic() {
    if (audioPlayer) {
        audioPlayer.pause();
        audioPlayer.src = '';
        audioPlayer = null;
    }
    const fileInput = document.getElementById('music-file');
    const playBtn = document.getElementById('music-play');
    const pauseBtn = document.getElementById('music-pause');
    fileInput.onchange = null;
    playBtn.onclick = null;
    pauseBtn.onclick = null;
}
// 音乐模块初始化函数（切换到音乐模块时执行）


// 页面关闭时释放资源（可选，避免内存泄漏）
window.onbeforeunload = () => {
    if (audioPlayer && audioPlayer.src) {
        URL.revokeObjectURL(audioPlayer.src); // 释放本地文件URL
    }
};